package com.neu.servlet;

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CookieDemo
 */
@WebServlet("/CookieDemo")
public class CookieDemo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CookieDemo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Servlet#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		/*
		 *  Cookie：是一个类，所以可以通过它的构造方法直接实例化 (构造方法与类名一致)
		 *  Cookie(String,String)：俩个参数用来描述存储到 Cookie 中的键值对
		 */
		Cookie c1 = new Cookie("username","tom");
		Cookie c2 = new Cookie("pass","1234");
		response.getWriter().write("开始给Cookie 添加数据。。。。。");
		/*
		 * 将生成的  Cookie 对象添加到响应对象中，通过响应的方法add();
		 * 	添加过程实际作用：就是通过响应对象将已经存储完数据的Cookic 对象发送给浏览器;
		 */
		response.addCookie(c1);
		response.addCookie(c2);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
